const { defineConfig } = require('@vue/cli-service')
const bodyParser = require('body-parser')
const Mock = require('mockjs')
const data = Mock.mock({
  'carList|100': [
    {
      'id|+1': 1,
      publishTime: '@date()',
      img: '@image(150x150, @color(), @city())',
      count: 0,
      collectFlag: false,
      distance: '@integer(1, 100)', // 距离远近
      mileage: '@integer(0.1, 10)', // 里程
      title: '宝马三系_' + '@ctitle()', //
      desc: '@cword(30)', // 车辆描述
      carAge: '@integer(1, 5)', // 车龄
      price: '@integer(20, 100)', // 车系价格
      'type|+1': [1, 0], // 1. 有货 0: 没有货
    },
  ],
})
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares: (middlewares, { app }) => {
      // 处理post参数
      app.use(bodyParser.json())

      // 登陆
      app.post('/api/login', (req, res) => {
        const { username, password } = req.body
        res.json({ code: 0, data: { token: `${username}_admin` } })
      })

      // 分页接口，上拉加载
      app.get('/api/car', (req, res) => {
        const { page = 1, size = 10 } = req.query
        res.json({
          code: 0,
          msg: 'success',
          data: {
            total: data.carList.length,
            list: data.carList.slice((page - 1) * size, page * size),
          },
        })
      })

      // 详情接口
      app.get('/api/detail', (req, res) => {
        const { id } = req.query
        console.log('id:', id)
        res.json({
          code: 0,
          msg: 'success',
          data: data.carList.filter((v) => v.id == id),
        })
      })

      return middlewares
    },
  },
})
